package net.wofly.sale.report.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import net.wofly.common.util.DateUtil;
import net.wofly.sale.report.domain.OutStockDTO;
import net.wofly.sale.report.domain.ResourceAskDTO;
import net.wofly.sale.report.domain.StockDTO;
import net.wofly.sale.stock.service.IOutStockService;
import net.wofly.sale.stock.service.IResourceAskService;
import net.wofly.sale.stock.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.wofly.sale.report.repository.SaleStockDailyReportRepository;
import net.wofly.sale.report.domain.SaleStockDailyReport;
import net.wofly.sale.report.service.ISaleStockDailyReportService;

@Service("iSaleStockDailyReportService")
@Transactional
public class SaleStockDailyReportServiceImpl implements ISaleStockDailyReportService {

	@Autowired
	SaleStockDailyReportRepository saleStockDailyReportRepository;
	@Autowired
	IResourceAskService iResourceAskService;
	@Autowired
	IStockService iStockService;
	@Autowired
	IOutStockService iOutStockService;

	@Override
	public SaleStockDailyReport save(SaleStockDailyReport entity) {
		return saleStockDailyReportRepository.save(entity);
	}

	@Override
	public void delete(SaleStockDailyReport entity) {
		saleStockDailyReportRepository.delete(entity);
	}

	@Override
	public void delete(String key) {
		saleStockDailyReportRepository.delete(key);
	}

	@Override
	public SaleStockDailyReport findOne(String key) {
		return saleStockDailyReportRepository.findOne(key);
	}

	@Override
	public List<SaleStockDailyReport> save(List<SaleStockDailyReport> entityList) {
		return (List<SaleStockDailyReport>) saleStockDailyReportRepository.save(entityList);
	}
	
	/**
	 * 根据时间生成日报
	 *
	 * @param countDate
	 */
	@Override
	public void generateSaleStockDailyReport(Long countDate) {
		saleStockDailyReportRepository.deleteByCountDate(countDate);

		long countEndTime = DateUtil.processEndTime(new Date(countDate)).getTime();
		List<ResourceAskDTO> resourceAskDTOs = iResourceAskService.findByCountDate(countDate, countEndTime);
		List<StockDTO> stockDTOs = iStockService.findByCountDate(countDate, countEndTime);
		List<OutStockDTO> outStockDTOs = iOutStockService.findByCountDate(countDate, countEndTime);
		//前一天的数据
		List<SaleStockDailyReport> saleStockDailyReports = saleStockDailyReportRepository.findByCountDate(DateUtil.getAddDate(new Date(countDate), -1).getTime());

		Map<String, SaleStockDailyReport> reportMap = new HashMap<>();
		for (ResourceAskDTO resourceAskDTO : resourceAskDTOs) {
			Integer ask = resourceAskDTO.getAsk() == null ? 0 : resourceAskDTO.getAsk().intValue();
			SaleStockDailyReport saleStockDailyReport = new SaleStockDailyReport(ask, resourceAskDTO.getCommodityTerminalID(),
					resourceAskDTO.getCommodityTerminalName(), countDate, resourceAskDTO.getAskUserID(), resourceAskDTO.getAskUserName());
			String key = resourceAskDTO.getAskUserID() + ":" + resourceAskDTO.getCommodityTerminalID();
			reportMap.put(key, saleStockDailyReport);
		}
		for (StockDTO stockDTO : stockDTOs) {
			String key = stockDTO.getStockUserID() + ":" + stockDTO.getCommodityTerminalID();
			Integer stock = stockDTO.getStock() == null ? 0 : stockDTO.getStock().intValue();
			if (reportMap.get(key) != null) {
				reportMap.get(key).setStock(stock);
			} else {
				SaleStockDailyReport saleStockDailyReport = new SaleStockDailyReport(stockDTO.getCommodityTerminalID(),
						stockDTO.getCommodityTerminalName(), stock, countDate, stockDTO.getStockUserID());
				reportMap.put(key, saleStockDailyReport);
			}
		}
		for (OutStockDTO outStockDTO : outStockDTOs) {
			String key = outStockDTO.getSaleUserID() + ":" + outStockDTO.getCommodityTerminalID();
			Integer sale = outStockDTO.getSale() == null ? 0 : outStockDTO.getSale().intValue();
			if (reportMap.get(key) != null) {
				reportMap.get(key).setSale(sale);
			} else {
				SaleStockDailyReport saleStockDailyReport = new SaleStockDailyReport(outStockDTO.getCommodityTerminalID(),
						outStockDTO.getCommodityTerminalName(), countDate, sale, outStockDTO.getSaleUserID());
				reportMap.put(key, saleStockDailyReport);
			}
		}

		for (SaleStockDailyReport oldReport : saleStockDailyReports) {
			String key = oldReport.getUserID() + ":" + oldReport.getCommodityTerminalID();
			Integer stock = oldReport.getStock();
			if (reportMap.get(key) != null) {
				reportMap.get(key).setHisStock(stock);
			} else {
				SaleStockDailyReport saleStockDailyReport = new SaleStockDailyReport(oldReport.getCommodityTerminalID(),
						oldReport.getCommodityTerminalName(), stock, countDate, oldReport.getUserID());
				reportMap.put(key, saleStockDailyReport);
			}
		}
		saleStockDailyReportRepository.save(reportMap.values());
	}

	@Override
	public List<SaleStockDailyReport> findByUserIDAndCountDate(String userID, Long countDate) {
		return saleStockDailyReportRepository.findByUserIDAndCountDate(userID, countDate);
	}




}
